package com.neusoft.neuiotms.dcom.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.KnowledgeCommentDTO;
import com.neusoft.neuiotms.dcom.model.KnowledgeComment;
import com.neusoft.neuiotms.dcom.repository.KnowledgeCommentRepository;

@Component
public class KnowledgeCommentConverter extends AbstractConverter<KnowledgeComment, KnowledgeCommentDTO> {

    @Autowired
    private KnowledgeCommentRepository knowledgeCommentRepository;

    @Override
    public KnowledgeComment toModel(final KnowledgeCommentDTO dto) {
        KnowledgeComment model = new KnowledgeComment();
        if (dto.isNew()) {
            model = new KnowledgeComment();
        } else {
            model = this.knowledgeCommentRepository.getOne(dto.getId());
        }
        model.setApprove(dto.getApprove());
        model.setContent(dto.getContent());
        model.setOpposition(dto.getOpposition());
        model.setUserCode(dto.getUserCode());
        model.setUserName(dto.getUserName());
        return model;
    }

    @Override
    public KnowledgeCommentDTO toDTO(final KnowledgeComment model, final boolean forListView) {
        final KnowledgeCommentDTO dto = new KnowledgeCommentDTO();
        dto.setApprove(model.getApprove());
        dto.setContent(model.getContent());
        dto.setId(model.getId());
        dto.setOpposition(model.getOpposition());
        dto.setUserCode(model.getUserCode());
        dto.setUserName(model.getUserName());
        dto.setCreateTime(model.getCreateTime());
        return dto;
    }

}
